package cn.e4j.modular.system.controller;

import cn.e4j.config.properties.E4jProperties;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.FileUtil;
import cn.e4j.core.util.ToolUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 上传
 *
 * @author wenchuan.li
 * @date 2019/01/08 09:30
 */
@Controller
@RequestMapping("/upload")
@Slf4j
public class UploadController extends BaseController {

    @Autowired
    private E4jProperties e4jProperties;

    /**
     * 上传图片
     */
    @RequestMapping(method = RequestMethod.POST, path = "/imgs")
    @ResponseBody
    public ResponseData imgs(HttpServletRequest request) {
        List<Map<String, String>> paths;
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        try {
            paths = FileUtil.saveFiles(files, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData(BizExceptionEnum.UPLOAD_ERROR.getMessage());
        }
        return new SuccessResponseData(paths);
    }

    /**
     * 上传文件
     */
    @RequestMapping(method = RequestMethod.POST, path = "/files")
    @ResponseBody
    public ResponseData files(HttpServletRequest request) {
        List<Map<String, String>> paths;
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        String basePath = null;
        if (ToolUtil.isNotEmpty(request.getParameter("basePath"))) {
            String rootPath = ToolUtil.getWebRootPath(null);
            rootPath = rootPath.substring(0, rootPath.lastIndexOf("/"));
            basePath = rootPath + File.separator + e4jProperties.getFileUploadPath() + File.separator + request.getParameter("basePath");
        }
        try {
            paths = FileUtil.saveFiles(files, basePath);
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData(BizExceptionEnum.UPLOAD_ERROR.getMessage());
        }
        return new SuccessResponseData(paths);
    }

    /**
     * 富文本上传图片
     */
    @RequestMapping(method = RequestMethod.POST, path = "/imgsEdit")
    @ResponseBody
    public String imgsEdit(HttpServletRequest request) {
        List<Map<String, String>> paths;
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        try {
            paths = FileUtil.saveFiles(files, null);
        } catch (Exception e) {
            e.printStackTrace();
            return BizExceptionEnum.UPLOAD_ERROR.getMessage();
        }
        String end = "{\"code\": 1,\"msg\": \"服务器故障\",\"data\": {\"src\": \"\"}}"; //返回的jso
        for (int i = 0; i < paths.size(); i++) {
            String serverPath = "/wissun-smfile/" + paths.get(i).get("name");
            end = "{\"code\": 0,\"msg\": \"成功\",\"data\": {\"src\": \"" + serverPath + "\"}}";
        }
        return end;
    }

}
